<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>String operators</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Strings</a> &gt; <a href=
    "string_operators.html">String operators</a>
  </div>

  <div class="name-format">
    String operators
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string&gt;
  bool operator==(const string&amp; c1, const string&amp; c2);
  bool operator!=(const string&amp; c1, const string&amp; c2);
  bool operator&lt;(const string&amp; c1, const string&amp; c2);
  bool operator&gt;(const string&amp; c1, const string&amp; c2);
  bool operator&lt;=(const string&amp; c1, const string&amp; c2);
  bool operator&gt;=(const string&amp; c1, const string&amp; c2);
  string operator+(const string&amp; s1, const string&amp; s2 );
  string operator+(const char* s, const string&amp; s2 );
  string operator+( char c, const string&amp; s2 );
  string operator+( const string&amp; s1, const char* s );
  string operator+( const string&amp; s1, char c );
  ostream&amp; operator&lt;&lt;( ostream&amp; os, const string&amp; s );
  istream&amp; operator&gt;&gt;( istream&amp; is, string&amp; s );
  string&amp; operator=( const string&amp; s );
  string&amp; operator=( const char* s );
  string&amp; operator=( char ch );
  char&amp; operator[]( <strong>size_type</strong> index );
</pre>

  <p>C++ strings can be compared and assigned with the standard
  comparison operators: ==, !=, &lt;=, &gt;=, &lt;, &gt;, and =.
  Performing a comparison or assigning one string to another takes
  <a href="../complexity.html">linear time</a>.</p>

  <p>Two strings are equal if:</p>
  <pre class="example-code">
  1. Their size is the same, and
  2. Each member in location i in one string is equal to the the member in location i in the other string.              
</pre>

  <p>Comparisons among strings are done lexicographically.</p>

  <p>In addition to these normal (C++ Multimaps) <a href=
  "../cppmultimap/multimap_operators.html">Multimap operators</a>,
  strings can also be concatenated with the + operator and fed to the
  C++ I/O stream classes with the &lt;&lt; and &gt;&gt; operators.</p>

  <p>For example, the following code concatenates two strings and
  displays the result:</p>
  <pre class="example-code">
 string s1 = &quot;Now is the time...&quot;;
 string s2 = &quot;for all good men...&quot;;
 string s3 = s1 + s2;
 cout &lt;&lt; &quot;s3 is &quot; &lt;&lt; s3 &lt;&lt; endl;            
</pre>

  <p>Futhermore, strings can be assigned values that are other
  strings, character arrays, or even single characters. The following
  code is perfectly valid:</p>
  <pre class="example-code">
 char ch = &#39;N&#39;;
 string s;
 s = ch;         
</pre>

  <p>Individual characters of a string can be examined with the []
  operator, which runs in <a href="../complexity.html">constant
  time</a>.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    (C++ Multimaps) <a href=
    "../cppmultimap/multimap_operators.html">Multimap
    operators</a><br>
    <a href="c_str.html">c_str</a><br>
    <a href="compare.html">compare</a><br>
    <a href="data.html">data</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
